var http = require('http');
var fs = require("fs");
var url = require("url");
var tools = require('./tools');
var path = require('path'); //node_modules\path\path.js
var ejs = require('ejs');  //node_modules\node_modules\ejs\ejs.js
console.log(":");


http.createServer(function(req,res){
    let static = 'test/html';
    tools.static(req,res,static);
      
    let pathname = url.parse(req.url).pathname;
}).listen(8081)
// tools.getFileMime('.png');
// console.log(tools.getFileMime('.png'))
/*
http.createServer(function (req, res) {
    var static = 'test/html/'
    tools.static(req,res,static)
    let pathname = url.parse(req.url).pathname

    console.log(req.method)   //请求类型   
    //  GET请求 用于获取数据   POST请求 用于提交数据

    switch(pathname){
        case '/login':
            
            let msg = 'data from database'
            let list = [
                { title: '111', name: 'aaa'},
                { title: '222', name: 'bbb'},
                { title: '333', name: 'ccc'},
                { title: '444', name: 'ddd'},
            ]
            // test\views\login.ejs  
            ejs.renderFile('test/views/login.ejs',//{},
                {
                    msg:msg,
                    list:list
                },
            (err,data)=>{
                res.writeHead(200, { 'Content-Type': 'text/html;charset="utf-8"'})
                res.end(data)
            })
           
            break;
        case '/news':
            // var query = url.parsea(req.url,true).query;
            // console.log(query.page)
            var query = url.parse(req.url).query   //query ：string
            var query = url.parse(req.url, true).query   //query ：object
            console.log(query, typeof query)
            console.log(query.page)
            res.writeHead(200, { 'Content-Type': 'text/html;charset="utf-8"'})
            res.end('get success!')
            break;
        case '/doLogin':
            // 获取post传值
            var postData = "";
            // 数据块接收
            req.on('data', function(postDataChunk){
                postData += postDataChunk;
            })
            // 数据块接受完毕，执行回调函数
            req.on('end', function(){
                try {
                    console.log(postData, typeof postData)
                    // postData = JSON.parse(postData);
                    res.end(postData)
                } catch (error) {
                    // req.query = postData;
                    // console.log(querystring.parse(postData));
                }
            })
            res.writeHead(200, { 'Content-Type': 'text/html;charset="utf-8"'})
            // res.end("register")
            break;
        case '/admin':
            res.writeHead(200, { 'Content-Type': 'text/html;charset="utf-8"'})
            res.end("admin")
            break;
        default:
            res.writeHead(404, { 'Content-Type': 'text/html;charset="utf-8"'})
            res.end("404")
    }
}).listen(8081)
*/

/*
http.createServer(function (request, response) {
//获取地址 test\html\电影卡片练习.html
    let pathname = url.parse(request.url).pathname;
    pathname = pathname == '/'?'index.html':pathname;   //设置默认为/index.html
    let extname = path.extname(pathname)  // path.extname()获取地址后缀
//通过fs模块读取文件
    if(pathname!='/favicon.ico'){
        // console.log(url.parse(request.url))
        // fs.readFile('test/' + pathname, async (err,data)=>{  异步
        fs.readFile('test/' + pathname, (err,data)=>{    //同步
            if(err){
                console.log(err)
                response.writeHead(404,{'Content-Type': 'text/html;charset="utf-8"'})
                response.end('这个页面不存在')
            }
            // let mime = await tools.getFileMime(extname)   异步
            let mime = tools.getFileMime(extname)   //同步
            // console.log(mime)
            response.writeHead(200, {'Content-Type': mime + ';charset="utf-8"'});
            response.end(data);
        });
    }
}).listen(8082);
*/

// console.log('Server running at http://127.0.0.1:8081/');